﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPNotifications.Layouts.SPNotifications
{
    public partial class SubmitNotification : LayoutsPageBase
    {
        #region ..:: FIELDS ::..
        protected Literal litMessages;
        protected TextBox txtTitle;
        protected TextBox txtDesc;
        protected PeopleEditor PeopleEditorAdmins;

        private SPWeb web;
        private SPList list;

        #endregion ..:: FIELDS ::..

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Clean controls
                CleanControls();
            }
        }

        protected void BtnSubmitClicked(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {

                    using (SPSite site = new SPSite(this.Site.Url))
                    using (SPWeb web = site.OpenWeb(this.Web.ServerRelativeUrl))
                    {
                        this.web = web;

                        // Get notifications list
                        list = web.GetList("/Lists/Notifications");

                        // Loop thru items in peoplepicker
                        foreach (PickerEntity p in PeopleEditorAdmins.Entities)
                        {
                            // Check if entity is user or SharePoint Group
                            if (isEntityUser(p))
                            {
                                // Add a notification for each user
                                AddNotification(p.Description);

                            } // end - if
                            else
                            { 
                                // Assuming if entity not user, then it is SharePoint group
                                SPGroup group = web.Groups[p.Description];

                                foreach (SPUser user in group.Users)
                                {
                                    // Add a notification for each user
                                    AddNotification(user.LoginName);

                                } // end - foreach
                            
                            } // end - else

                        } // end - foreach

                    } //end - using

                    // Display info message
                    litMessages.Text = "Notification(s) created successfully!";

                    // Clean controls
                    CleanControls();

                } // end - try
                catch (Exception ex)
                {
                    litMessages.Text = ex.ToString();
                
                }

            } // end - page.isvalied

        } // end - BtnSubmitClicked

        #region Helper Methods

        private bool isEntityUser (PickerEntity entity)
        {
            if (entity.EntityData["PrincipalType"].ToString() == "User")
                return true;
            else
                return false;
        } // end - isEntityUser

        private void AddNotification (string logonName)
        {
            // Add a notification for a user
            SPListItem item = list.AddItem();
            item["Title"] = txtTitle.Text;
            item["NotificationDesc"] = txtDesc.Text;
            item["TargetedTo"] = web.EnsureUser(logonName);
            item["ReadStatus"] = "Unread";
            item.Update();

        } // end - AddNotification

        private void CleanControls()
        {
            // Clean controls
            txtTitle.Text = string.Empty;
            txtDesc.Text = string.Empty;
            PeopleEditorAdmins.Entities.Clear();
        }

        #endregion

        
    }
}
